<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  

  
  <title>redis05-在项目中搭建读写分+高可用+多master的redis cluster集群 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="redis cluster最最基础的一些知识 redis cluster: 自动，master+slave复制和读写分离，master+slave高可用和主备切换，支持多个master的hash slot支持数据分布式存储">
<meta name="keywords" content="redis">
<meta property="og:type" content="article">
<meta property="og:title" content="redis05-在项目中搭建读写分+高可用+多master的redis cluster集群">
<meta property="og:url" content="http://yoursite.com/2018/11/12/redis05-在项目中搭建读写分-高可用-多master的redis-cluster集群/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="redis cluster最最基础的一些知识 redis cluster: 自动，master+slave复制和读写分离，master+slave高可用和主备切换，支持多个master的hash slot支持数据分布式存储">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2018-11-12T14:46:34.513Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="redis05-在项目中搭建读写分+高可用+多master的redis cluster集群">
<meta name="twitter:description" content="redis cluster最最基础的一些知识 redis cluster: 自动，master+slave复制和读写分离，master+slave高可用和主备切换，支持多个master的hash slot支持数据分布式存储">
  
    <link rel="alternate" href="/org/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/org/css/style.css">
</head>
</html>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/org/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/org/">Home</a>
        
          <a class="main-nav-link" href="/org/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/org/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-redis05-在项目中搭建读写分-高可用-多master的redis-cluster集群" class="article article-type-post" itemscope="" itemprop="blogPost">
  <div class="article-meta">
    <a href="/org/2018/11/12/redis05-在项目中搭建读写分-高可用-多master的redis-cluster集群/" class="article-date">
  <time datetime="2018-11-12T14:45:49.000Z" itemprop="datePublished">2018-11-12</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      redis05-在项目中搭建读写分+高可用+多master的redis cluster集群
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>redis cluster最最基础的一些知识</p>
<p>redis cluster: 自动，master+slave复制和读写分离，master+slave高可用和主备切换，支持多个master的hash slot支持数据分布式存储</p>
<a id="more"></a>
<p>停止之前所有的实例，包括redis主从和哨兵集群</p>
<p>1、redis cluster的重要配置</p>
<p>cluster-enabled &lt;yes/no&gt;</p>
<p>cluster-config-file <filename>：这是指定一个文件，供cluster模式下的redis实例将集群状态保存在那里，包括集群中其他机器的信息，比如节点的上线和下限，故障转移，不是我们去维护的，给它指定一个文件，让redis自己去维护的</filename></p>
<p>cluster-node-timeout <milliseconds>：节点存活超时时长，超过一定时长，认为节点宕机，master宕机的话就会触发主备切换，slave宕机就不会提供服务</milliseconds></p>
<p>2、在三台机器上启动6个redis实例</p>
<p>（1）在eshop-cache03上部署目录</p>
<p>/etc/redis（存放redis的配置文件），/var/redis/6379（存放redis的持久化文件）</p>
<p>（2）编写配置文件</p>
<p>redis cluster集群，要求至少3个master，去组成一个高可用，健壮的分布式的集群，每个master都建议至少给一个slave，3个master，3个slave，最少的要求</p>
<p>正式环境下，建议都是说在6台机器上去搭建，至少3台机器</p>
<p>保证，每个master都跟自己的slave不在同一台机器上，如果是6台自然更好，一个master+一个slave就死了</p>
<p>3台机器去搭建6个redis实例的redis cluster</p>
<p>mkdir -p /etc/redis-cluster<br>mkdir -p /var/log/redis<br>mkdir -p /var/redis/7001</p>
<p>port 7001<br>cluster-enabled yes<br>cluster-config-file /etc/redis-cluster/node-7001.conf<br>cluster-node-timeout 15000<br>daemonize    yes<br>pidfile        /var/run/redis_7001.pid<br>dir         /var/redis/7001<br>logfile /var/log/redis/7001.log<br>bind 192.168.31.187<br>appendonly yes</p>
<p>至少要用3个master节点启动，每个master加一个slave节点，先选择6个节点，启动6个实例</p>
<p>将上面的配置文件，在/etc/redis下放6个，分别为: 7001.conf，7002.conf，7003.conf，7004.conf，7005.conf，7006.conf</p>
<p>（3）准备生产环境的启动脚本</p>
<p>在/etc/init.d下，放6个启动脚本，分别为: redis_7001, redis_7002, redis_7003, redis_7004, redis_7005, redis_7006</p>
<p>每个启动脚本内，都修改对应的端口号</p>
<p>（4）分别在3台机器上，启动6个redis实例</p>
<p>将每个配置文件中的slaveof给删除</p>
<p>3、创建集群</p>
<p>下面方框内的内容废弃掉</p>
<p>=======================================================================</p>
<p>wget <a href="https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz" target="_blank" rel="noopener">https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz</a><br>tar -zxvf ruby-2.3.1.tar.gz<br>./configure -prefix=/usr/local/ruby<br>make &amp;&amp; make install<br>cd /usr/local/ruby<br>cp bin/ruby /usr/local/bin<br>cp bin/gem /usr/local/bin</p>
<p>wget <a href="http://rubygems.org/downloads/redis-3.3.0.gem" target="_blank" rel="noopener">http://rubygems.org/downloads/redis-3.3.0.gem</a><br>gem install -l ./redis-3.3.0.gem<br>gem list –check redis gem</p>
<p>=======================================================================</p>
<p>因为，以前比如公司里面搭建集群，公司里的机器的环境，运维会帮你做好很多事情</p>
<p>在讲课的话，我们手工用从零开始装的linux虚拟机去搭建，那肯定会碰到各种各样的问题</p>
<p>yum install -y ruby<br>yum install -y rubygems<br>gem install redis</p>
<p>cp /usr/local/redis-3.2.8/src/redis-trib.rb /usr/local/bin</p>
<p>redis-trib.rb create –replicas 1 192.168.31.187:7001 192.168.31.187:7002 192.168.31.19:7003 192.168.31.19:7004 192.168.31.227:7005 192.168.31.227:7006</p>
<p>–replicas: 每个master有几个slave</p>
<p>6台机器，3个master，3个slave，尽量自己让master和slave不在一台机器上</p>
<p>yes</p>
<p>redis-trib.rb check 192.168.31.187:7001</p>
<p>4、读写分离+高可用+多master</p>
<p>读写分离：每个master都有一个slave<br>高可用：master宕机，slave自动被切换过去<br>多master：横向扩容支持更大数据量</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/11/12/redis05-在项目中搭建读写分-高可用-多master的redis-cluster集群/" data-id="cjoztxu7m00351wijjvww9e88" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/org/tags/redis/">redis</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/org/2018/11/13/redis06-cluster实现高可用性/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          redis06-cluster实现高可用性
        
      </div>
    </a>
  
  
    <a href="/org/2018/11/05/在项目中用经典的三节点方式部署哨兵集群-笔记/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">在项目中用经典的三节点方式部署哨兵集群-笔记</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/SVN/">SVN</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/Spring-secrity/">Spring-secrity</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/hexo/">hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/hibenrate/">hibenrate</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/jekyll/">jekyll</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/jenkins/">jenkins</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/redis/">redis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/shiro/">shiro</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/spingMVC/">spingMVC</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/spring-cloud/">spring cloud</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/spring-cloud/">spring-cloud</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/spring-secrity/">spring-secrity</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/springMVC/">springMVC</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/springboot/">springboot</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/生活/">生活</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/org/tags/SVN/" style="font-size: 12px;">SVN</a> <a href="/org/tags/Spring-secrity/" style="font-size: 10px;">Spring-secrity</a> <a href="/org/tags/hexo/" style="font-size: 10px;">hexo</a> <a href="/org/tags/hibenrate/" style="font-size: 10px;">hibenrate</a> <a href="/org/tags/jekyll/" style="font-size: 10px;">jekyll</a> <a href="/org/tags/jenkins/" style="font-size: 10px;">jenkins</a> <a href="/org/tags/redis/" style="font-size: 16px;">redis</a> <a href="/org/tags/shiro/" style="font-size: 18px;">shiro</a> <a href="/org/tags/spingMVC/" style="font-size: 10px;">spingMVC</a> <a href="/org/tags/spring-cloud/" style="font-size: 10px;">spring cloud</a> <a href="/org/tags/spring-cloud/" style="font-size: 10px;">spring-cloud</a> <a href="/org/tags/spring-secrity/" style="font-size: 12px;">spring-secrity</a> <a href="/org/tags/springMVC/" style="font-size: 14px;">springMVC</a> <a href="/org/tags/springboot/" style="font-size: 20px;">springboot</a> <a href="/org/tags/生活/" style="font-size: 10px;">生活</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/org/archives/2018/11/">November 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/org/archives/2018/10/">October 2018</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/org/2018/11/27/redis07-zookeeper-kafka集群部署以及如何使用简单介绍/">redis07-zookeeper+kafka集群部署以及如何使用简单介绍</a>
          </li>
        
          <li>
            <a href="/org/2018/11/13/redis06-cluster实现高可用性/">redis06-cluster实现高可用性</a>
          </li>
        
          <li>
            <a href="/org/2018/11/12/redis05-在项目中搭建读写分-高可用-多master的redis-cluster集群/">redis05-在项目中搭建读写分+高可用+多master的redis cluster集群</a>
          </li>
        
          <li>
            <a href="/org/2018/11/05/在项目中用经典的三节点方式部署哨兵集群-笔记/">在项目中用经典的三节点方式部署哨兵集群-笔记</a>
          </li>
        
          <li>
            <a href="/org/2018/11/05/redis哨兵的多个核心底层原理-笔记/">redis哨兵的多个核心底层原理-笔记</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2018 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/org/" class="mobile-nav-link">Home</a>
  
    <a href="/org/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//code.jquery.com/jquery-2.0.3.min.js"></script>


  <link rel="stylesheet" href="/org/fancybox/jquery.fancybox.css">
  <script src="/org/fancybox/jquery.fancybox.pack.js"></script>


<script src="/org/js/script.js"></script>



  </div>
</body>
</html>